package com.crm.jdy.controller.usermanager;

import com.crm.jdy.common.dto.CatalogueInfoDto;
import com.crm.jdy.common.pojo.CatalogueInfo;
import com.crm.jdy.common.utils.api.ResultVo;
import com.crm.jdy.common.utils.currentlimiting.RateLimit;
import com.crm.jdy.service.usermanager.CatalogueInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
*
* <p>说明： 目录信息API接口层</P>
* @version: V1.0
* @author: kong
* @time    2021年4月6日
*
*/
@Api(tags = "catalogueInfo", description = "目录信息")
@RestController
@RequestMapping("/catalogueInfo")
public class CatalogueInfoController{

    @Reference(version = "${service.version}", check = false)
    CatalogueInfoService catalogueInfoService;

    @RateLimit()
    @ApiOperation("目录信息")
    @PostMapping("/seleteCatalogueInfo")
    public ResultVo seleteCatalogueInfo(@RequestBody CatalogueInfoDto catalogueInfoDto){
        PageInfo<CatalogueInfo> list = catalogueInfoService.seleteCatalogueInfo(catalogueInfoDto);
        return  ResultVo.success(list);
    }

    @RateLimit()
    @ApiOperation("更新目录信息")
    @PostMapping("/updateCatalogueInfo")
    public ResultVo updateCatalogueInfo(@ApiParam(value = "catalogueInfo", required = true)
                                   @Validated @RequestBody CatalogueInfo catalogueInfo){
        boolean index = catalogueInfoService.updateCatalogueInfo(catalogueInfo);
        if(index){
            return  ResultVo.success();
        }
        return  ResultVo.failed();
    }

    @RateLimit()
    @ApiOperation("添加目录信息")
    @PostMapping("/addCatalogueInfo")
    public ResultVo addCatalogueInfo(@ApiParam(name = "catalogueInfo", required = false)
                                @Validated @RequestBody CatalogueInfo catalogueInfo){
        boolean list  = catalogueInfoService.addCatalogueInfo(catalogueInfo);
        if(list){
            return ResultVo.success("添加成功");
        }
        return ResultVo.failed("添加失败，数据已存在");
    }

    @RateLimit()
    @ApiOperation("删除目录信息")
    @DeleteMapping("deleteCatalogueInfo/{cauid}")
    public ResultVo deleteCatalogueInfo(@NotNull @ApiParam(name = "cauid", value = "cauid", required = true) @PathVariable String cauid){
        boolean b = catalogueInfoService.removeCatalogueInfo(cauid);
        if(b){
            return ResultVo.success();
        }
        return ResultVo.failed();
    }

    @RateLimit()
    @ApiOperation("根据用户名查询目录信息")
    @GetMapping("getCatalogueInfo/{userName}")
    public ResultVo getCatalogueInfo(@NotNull @ApiParam(name = "userName", value = "userName", required = true) @PathVariable String userName){
        List<CatalogueInfo>  infoList = catalogueInfoService.getCatalogueInfo(userName);
        if(infoList.size() > 0){
            return ResultVo.success(infoList);
        }
        return ResultVo.failed();
    }

}